/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Connect;
import modele.Dep;
import modele.Med;
import modele.Pays;
import modele.Spe;

/**
 *
 * @author sio
 */
public class Control extends HttpServlet {

    private Pays p;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays(); //instancie les objets utiles
    }

    public void destroy() {
        Connect.close();
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String action = request.getParameter("action");
        String page;

        page = "index.jsp";

        if ("listeMedParDep".equals(action)) {

            String envoie = request.getParameter("choixDep");

            if (envoie == null) {
                request.setAttribute("lesDeps", p.getLesDeps());
                page = "choixDep.jsp";

            } else {
                String choixDep = request.getParameter("choixDep");

                Dep d = p.getLeDep(choixDep);
                request.setAttribute("medecins", d.getLesMeds());
                page = "medParDep.jsp";
            }
        }

        if ("listeMedParNom".equals(action)) {

            String envoie = request.getParameter("nom");

            if (envoie == null) {
                page = "choixNom.jsp";

            } else {
                request.setAttribute("recherche", envoie);
                Collection<Med> lesMeds = new TreeSet<Med>();

                for (Dep d : p.getLesDeps()) {

                    for (Med unMed : d.getLesMeds()) {

                        if (unMed.getNom().startsWith(envoie)) {
                            lesMeds.add(unMed);
                        }
                    }
                }

                request.setAttribute("medecins", lesMeds);
                page = "medParNom.jsp";
            }
        }

        if ("listeMedParSpe".equals(action)) {

            String envoie = request.getParameter("ok");

            if (envoie == null) {
                request.setAttribute("lesSpes", p.getLesSpes());
                page = "choixSpe.jsp";

            } else {
                String choixSpe = request.getParameter("choixSpe");
                request.setAttribute("recherche", choixSpe);
                
                Spe s = p.getLaSpe(choixSpe);
                request.setAttribute("medecins", s.getLesMeds());
                page = "medParSpe.jsp";
            }
        }

        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
